package com.ts.api.http.receive.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.gitee.apanlh.util.valid.Assert;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.common.context.ApiContext;
import com.ts.api.common.util.ApiDictUtils;
import com.ts.api.exp.ApiMsgException;
import com.ts.api.http.receive.entity.apply.req.ApiReceiveApplyXiaoLongQianBaoV2;
import com.ts.api.http.receive.entity.dataexchange.req.ApiReceiveDataExchangeXiaoLongQianBaoV2;
import com.ts.api.module.api.convert.core.annotation.ApiReceive;
import com.ts.api.module.api.convert.core.annotation.ApiReceiveConvertType;
import com.ts.api.module.api.convert.receive.entity.ApiReceiveApply;
import com.ts.api.module.api.convert.receive.entity.ApiReceiveDecrypt;
import com.ts.api.module.api.convert.receive.entity.ApiReceiveNotice;
import com.ts.api.module.api.convert.receive.entity.ApiReceivePhoneMd5DataExchange;
import com.ts.api.module.api.convert.receive.handler.ApiReceivePhoneMd5ConvertHandler;
import com.ts.api.module.api.entity.ApiUserRequest;

import java.util.Map;

/**
 *  小龙钱包
 *
 * 链接：https://www.showdoc.com.cn/hrzd/11357309200337888
 * 密码：hrzd123#!
 *
 *  @author 李源聪
 */
@ApiReceive(apiClientId = "1930558554284904448",type = ApiReceiveConvertType.API_CHANNEL_PHONE_MD5)
public class ApiReceivePhoneMd5XiaoLongQianBaoV2 implements ApiReceivePhoneMd5ConvertHandler {

    private static final String KEY = "3309f1fc1dc8cef5";

    @Override
    public Map<String, String> requireParam(RequestVO requestVO) throws Exception {

        //  获取请求体参数
        Map<String, Object> map = JsonUtils.toMap(requestVO.getBody());
        String orgId = String.valueOf(map.get("orgId"));
        String data = String.valueOf(map.get("data"));

        //  传递什么，验证什么Key
        Assert.isNotEmptyThrows(orgId, new ApiMsgException(ApiEnum.API_RECEIVE_BODY_MISS.format("orgId")));
        Assert.isNotEmptyThrows(data, new ApiMsgException(ApiEnum.API_RECEIVE_BODY_MISS.format("data")));

        //  返回需要的数据Map
        return MapUtils.newHashMap(newMap -> {
            newMap.put("orgId", orgId);
            newMap.put("data", data);
        });
    }

    @Override
    public ApiReceiveDecrypt decrypt(Map<String, String> paramMap, RequestVO requestVO) throws Exception {
        String data = paramMap.get("data");
        AES aes = AES.createEcb(KEY.getBytes());
        String decrypt = aes.decryptFromBase64Str(data);
        return new ApiReceiveDecrypt(decrypt);
    }

    @Override
    public ApiUserRequest parseDataExchange(ApiReceiveDecrypt apiReceiveDecrypt) throws Exception {
        ApiReceiveDataExchangeXiaoLongQianBaoV2 parse = apiReceiveDecrypt.parse(ApiReceiveDataExchangeXiaoLongQianBaoV2.class);
        //  拷贝成系统的实体类
        ApiUserRequest apiUserRequest = CopyUtils.copy(parse, ApiUserRequest.class);
        fieldMapping(apiUserRequest);
        return apiUserRequest;
    }

    @Override
    public Map<String, Object> respDataExchange(ApiReceivePhoneMd5DataExchange dataExchangeMessage) throws Exception {
        Map<String, Object> resultMap = MapUtils.newLinkedHashMap();
        boolean suc = dataExchangeMessage.hasBizSuc();
        if (!suc) {
            resultMap.put("code", -1);
            resultMap.put("msg", dataExchangeMessage.getBizMsg());
            return resultMap;
        }

        resultMap.put("code", 0);
        resultMap.put("msg", "操作成功");
        resultMap.put("data", MapUtils.newLinkedHashMap(dataMap -> {
            // 设置订单号
            dataMap.put("orderId", ApiContext.REQ_ID.get());
            // 设置跳转地址
            dataMap.put("redirectUrl", dataExchangeMessage.getCallUrl());
            // 设置价格
            dataMap.put("price", dataExchangeMessage.getPrice());
        }));

        return resultMap;
    }

    @Override
    public ApiUserRequest parseApply(ApiReceiveDecrypt apiReceiveDecrypt) throws Exception {
        ApiReceiveApplyXiaoLongQianBaoV2 parse = apiReceiveDecrypt.parse(ApiReceiveApplyXiaoLongQianBaoV2.class);
        //  拷贝成系统的实体类
        ApiUserRequest apiUserRequest = CopyUtils.copy(parse, ApiUserRequest.class);
        fieldMapping(apiUserRequest);
        return apiUserRequest;
    }

    @Override
    public Map<String, Object> respApply(ApiReceiveApply applyMessage) throws Exception {
        Map<String, Object> resultMap = MapUtils.newLinkedHashMap();
        boolean suc = applyMessage.hasBizSuc();
        if (!suc) {
            resultMap.put("code", -1);
            resultMap.put("msg", applyMessage.getBizMsg());
            return resultMap;
        }
        resultMap.put("code", 0);
        resultMap.put("msg", "SUCCESS");
        return resultMap;
    }

    @Override
    public void fieldMapping(ApiUserRequest apiUserRequest) {
        //年龄
        String age = ApiDictUtils.convertAge(apiUserRequest.getAge());
        apiUserRequest.setAge(age);

        // gender 性别 1:男 2:女 0:未知
        // sex 性别
        // 0 男
        // 1 女
        // 2 未知
        String sex = apiUserRequest.getSex();
        if (ValidParam.isNotEmpty(sex)) {
            String  value = ChooseEq.create()
                    .when(sex, "1", "0")
                    .when(sex, "2", "1")
                    .when(sex, "0", "2")
                    .end();
            apiUserRequest.setSex(value);
        }

        // profession 职业 1:上班族 2:自由职业 3:企业主 4:公务员/国企
        // career 职业
        // 1:上班族
        // 2:自由职业
        // 3:企业主
        // 4:公务员/国企
        String career = apiUserRequest.getCareer();
        if (ValidParam.isNotEmpty(career)) {
            String  value = ChooseEq.create()
                    .when(career, "1", "1")
                    .when(career, "2", "2")
                    .when(career, "3", "3")
                    .end();
            apiUserRequest.setCareer(value);
        }

        // loanAmount 贷款额度 1:30000-50000 2:50000-100000 3:100000-200000
        // loanAmount 贷款额度
        // 1:30000-50000
        // 2:50000-100000
        // 3:100000-200000
        String loanAmount = apiUserRequest.getLoanAmount();
        if (ValidParam.isNotEmpty(loanAmount)) {
            String  value = ChooseEq.create()
                    .when(loanAmount, "1", "1")
                    .when(loanAmount, "2", "2")
                    .when(loanAmount, "3", "3")
                    .when(loanAmount, "4", "4")
                    .end();
            apiUserRequest.setLoanAmount(value);
        }

        // zhima 芝麻分 1:700以上 2:650-700 3:600-650 4:无
        // sesameCredit 芝麻分
        // 1 600分以下
        // 2 600~649分
        // 3 650~699分
        // 4 700分以上
        String sesameCredit = apiUserRequest.getSesameCredit();
        if (ValidParam.isNotEmpty(sesameCredit)) {
            String value = ChooseEq.create()
                    .when(sesameCredit, "1", "4")
                    .when(sesameCredit, "2", "3")
                    .when(sesameCredit, "3", "2")
                    .when(sesameCredit, "4", "1")
                    .end();
            apiUserRequest.setSesameCredit(value);
        }

        // socialSecurity 社保 1:6个月以下 2:6-12个月 3:12个月以上 4:无
        // socialSecurity 社保缴纳
        // 1 无社保
        // 2 缴纳6个月以下
        // 3 缴纳6个月以上
        // 4 有社保
        String socialSecurity = apiUserRequest.getSocialSecurity();
        if (ValidParam.isNotEmpty(socialSecurity)) {
            String value = ChooseEq.create()
                    .when(socialSecurity, "1", "2")
                    .when(socialSecurity, "2", "3")
                    .when(socialSecurity, "4", "1")
                    .end();
            apiUserRequest.setSocialSecurity(value);
        }

        // providentFund 公积金 1:6个月以下 2:6-12个月 3:12个月以上 4:无
        // providentFund 公积金
        // 1 无公积金
        // 2 缴纳6个月以下
        // 3 缴纳6个月以上
        // 4 有公积金
        String providentFund = apiUserRequest.getProvidentFund();
        if (ValidParam.isNotEmpty(providentFund)) {
            String value = ChooseEq.create()
                    .when(providentFund, "1", "2")
                    .when(providentFund, "2", "3")
                    .when(providentFund, "4", "1")
                    .end();
            apiUserRequest.setProvidentFund(value);
        }

        // vehicle 车产 1:有 2:无
        // carInfo 车辆情况
        // 1 无车产
        // 2 有车不抵押
        // 3 有车可抵押
        // 4 有车产
        String carInfo = apiUserRequest.getCarInfo();
        if (ValidParam.isNotEmpty(carInfo)) {
            String value = ChooseEq.create()
                    .when(carInfo, "1", "4")
                    .when(carInfo, "2", "1")
                    .end();
            apiUserRequest.setCarInfo(value);
        }

        // house 房产 1:有 2:无
        // realEstateInfo 房产情况
        // 1 无房产
        // 2 有房不抵押
        // 3 有房可抵押
        // 4 有房产
        // 5 有按揭房产
        // 6 有全款房产
        String realEstateInfo = apiUserRequest.getRealEstateInfo();
        if (ValidParam.isNotEmpty(realEstateInfo)) {
            String value = ChooseEq.create()
                    .when(realEstateInfo, "1", "4")
                    .when(realEstateInfo, "2", "1")
                    .end();
            apiUserRequest.setRealEstateInfo(value);
        }
    }

    @Override
    public Object reqPullApplyNotice(ApiReceiveNotice apiReceiveNotice) {
        Map<String, Object> hashMap = MapUtils.newLinkedHashMap();
        hashMap.put("orderId", apiReceiveNotice.getReqId());
        return hashMap;
    }

    @Override
    public boolean respPullApplyNotice(String responseBody) {
        JSONObject jsonObject = JsonUtils.get(responseBody);
        Integer code = jsonObject.getInteger("code");
        return Eq.object(code, 0);
    }

    @Override
    public Object reqPushApplyNotice(ApiReceiveNotice apiReceiveNotice) {
        Map<String, Object> hashMap = MapUtils.newLinkedHashMap();
        hashMap.put("orderId", apiReceiveNotice.getReqId());
        return hashMap;
    }

    @Override
    public boolean respPushApplyNotice(String responseBody) {
        JSONObject jsonObject = JsonUtils.get(responseBody);
        Integer code = jsonObject.getInteger("code");
        return Eq.object(code, 0);
    }
}
